package org.dromara.code.controller

import jakarta.servlet.http.HttpServletResponse
import jakarta.validation.constraints.*
import cn.dev33.satoken.annotation.SaCheckPermission
import org.springframework.web.bind.annotation.*
import org.dromara.common.idempotent.annotation.RepeatSubmit
import org.dromara.common.log.annotation.Log
import org.dromara.common.web.core.BaseController
import org.dromara.common.mybatis.core.page.PageQuery
import org.dromara.common.core.domain.R
import org.dromara.common.core.validate.AddGroup
import org.dromara.common.core.validate.EditGroup
import org.dromara.common.log.enums.BusinessType
import org.dromara.common.excel.utils.ExcelUtil
import org.dromara.code.domain.vo.CodeProductVo
import org.dromara.code.domain.bo.CodeProductBo
import org.dromara.code.service.ICodeProductService
import org.springframework.validation.annotation.Validated
import org.dromara.common.mybatis.core.page.TableDataInfo

/**
 * 一物一码产品类目
 *
 * @author Blutter J
 * @date 2025-08-07
 */
@Validated
@RestController
@RequestMapping("/code/product")
class CodeProductController(
    private val codeProductService: ICodeProductService
): BaseController() {

    /**
     * 查询一物一码产品类目列表
     */
    @SaCheckPermission("code:product:list")
    @GetMapping("/list")
    fun list(codeProductBo: CodeProductBo, pageQuery: PageQuery): TableDataInfo<CodeProductVo> {
        return codeProductService.queryPageList(codeProductBo, pageQuery)
    }

    /**
     * 导出一物一码产品类目列表
     */
    @SaCheckPermission("code:product:export")
    @Log(title = "一物一码产品类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    fun export(codeProductBo: CodeProductBo, response: HttpServletResponse) {
        val list = codeProductService.queryList(codeProductBo)
        ExcelUtil.exportExcel(list, "一物一码产品类目", CodeProductVo::class.java, response)
    }

    /**
     * 获取一物一码产品类目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("code:product:query")
    @GetMapping("/{id}")
    fun getInfo(@NotNull(message = "主键不能为空")
                @PathVariable id: Long): R<CodeProductVo> {
        return R.ok(codeProductService.queryById(id))
    }

    /**
     * 新增一物一码产品类目
     */
    @SaCheckPermission("code:product:add")
    @Log(title = "一物一码产品类目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    fun add(@Validated(AddGroup::class) @RequestBody codeProductBo: CodeProductBo): R<Void> {
        return toAjax(codeProductService.insertByBo(codeProductBo))
    }

    /**
     * 修改一物一码产品类目
     */
    @SaCheckPermission("code:product:edit")
    @Log(title = "一物一码产品类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    fun edit(@Validated(EditGroup::class) @RequestBody codeProductBo: CodeProductBo): R<Void> {
        return toAjax(codeProductService.updateByBo(codeProductBo))
    }

    /**
     * 删除一物一码产品类目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("code:product:remove")
    @Log(title = "一物一码产品类目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    fun remove(@NotEmpty(message = "主键不能为空")
               @PathVariable ids: List<Long>): R<Void> {
        return toAjax(codeProductService.deleteWithValidByIds(ids, true))
    }
}
